﻿function $(id) {
	return document.getElementById(id);
}

Node.prototype.empty = function() {
	while (this.firstChild) {
		this.removeChild(this.firstChild);
	}			
};

Node.prototype.setText = function(text) {
	this.empty();
	this.appendChild(document.createTextNode(text));
};

Node.prototype.getText = function() {
	var text = '',
		child = this.firstChild;
	while (child) {
		if (child.nodeType == Node.TEXT_NODE) {
			text += child.nodeValue;
		}
		child = child.nextSibling;
	}
	return text;
};

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, '');
}

function saveVariable(name, value) {
	localStorage[name] = value;
}

function loadVariable(name) {
	return localStorage[name];
}

function saveJSON(name, value) {
	saveVariable(name, JSON.stringify(value));
}

function loadJSON(name) {
	return JSON.parse(loadVariable(name));
}

function getNotifications(callback) {
	var xhr = new XMLHttpRequest();
	xhr.open('GET', 'http://www.fitocracy.com/get_notifications/', true);
	//xhr.setRequestHeader('User-Agent', 'FitocracyNotificationsGoogleChromeExtension/1.0', true);
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			var parser = new DOMParser(),
				xmlDoc = parser.parseFromString('<ul>' + xhr.responseText + '</ul>', 'text/xml');
			callback(xmlDoc);
		}
	}
	xhr.send();
}

function saveOptions(value) {
	localStorage.options = JSON.stringify(value);
}

function loadOptions() {
	var options;
	if (localStorage.options == null) {
		localStorage.options = '{}';
	}
	options = JSON.parse(localStorage.options);
	
	options.notifierEnabled = 	options.hasOwnProperty('notifierEnabled') ? options.notifierEnabled : true;
	options.desktopNotifications = 	options.hasOwnProperty('desktopNotifications') ? options.desktopNotifications : true;
	options.display1RMInFeed =	options.hasOwnProperty('display1RMInFeed') ? options.display1RMInFeed : true;
	options.groupsDropdown =	options.hasOwnProperty('groupsDropdown') ? options.groupsDropdown : true;
	options.hideWorkouts =	options.hasOwnProperty('hideWorkouts') ? options.hideWorkouts : false;
	options.openNotificationsIn =	options.hasOwnProperty('openNotificationsIn') ? options.openNotificationsIn : 1;
	
	localStorage.options = JSON.stringify(options);
	
	return options;
}

function humanize_time(time_in, offset) {
    var now = new Date();
    var time;
    if(typeof(time_in) == "string")
        time = new Date(time_in);
    else
        time = time_in;
    if(Object.prototype.toString.call(time) !== '[object Date]' || isNaN(time.getTime()))
        return time_in;

    //if(offset && !is_local)
    //    now = now.addHours((now.getTimezoneOffset()/60)-6);

    var s = Math.floor((now - time) / 1000.0);

    if(s <= 0)
      return "just now";

    var i = Math.floor(s / 31536000.0);
    if (i > 1)
        return i+" years ago";
    else if(i == 1)
        return i+" year ago";

    i = Math.floor(s / 2592000.0);
    if (i > 1)
        return i + " months ago";
    else if(i == 1)
        return i + " month ago";

    i = Math.floor(s / 86400.0);
    if (i > 1)
        return i + " days ago";
    else if(i == 1)
        return i + " day ago";

    i = Math.floor(s / 3600.0);
    if (i > 1)
        return i + " hours ago";
    else if(i == 1)
        return i + " hour ago";

    i = Math.floor(s / 60.0);
    if (i > 1)
        return i + " minutes ago";
    else if(i == 1)
        return i + " minute ago";

    return Math.floor(s) + " seconds ago";
}